// pages/my/my.js
import { 
  ClassicModel
} from '../../models/classic.js'
import {
  BookModel
} from '../../models/book.js'
const classicModel = new ClassicModel()
const bookModel = new BookModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    authorized: false,
    userInfo: null,
    bookCount: 0,
    classics: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 判断用户是否授权
    // wx.getUserInfo({
    //   success: data => {
    //     console.log(data)
    //   }
    // }) // 老版本小程序
    this.userAuthorized()
    this.getMyBookCount()
    this.getMyFavor()
  },

  getMyBookCount() { // 获取我喜欢的期刊数
    bookModel.getMyBookCount()
    .then(res => {
      this.setData({
        bookCount: res.count
      })
    })
  },

  getMyFavor() { // 获取我喜欢的书刊列表
    classicModel.getMyFavor(res => {
      this.setData({
        classics: res
      })
    })
  },

  userAuthorized() { // 判断用户是否授权
    wx.getSetting({
      success: data => {
        if(data.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: data => {
              this.setData({
                authorized: true,
                userInfo: data.userInfo
              })
            }
          })
        }
      }
    })
  },

  getUserInfo(event) {

  },

  onGetUserInfo(event) {
    const userInfo = event.detail.userInfo
    if(userInfo) {
      this.setData({
        userInfo,
        authorized: true
      })
    }
  },

  onJumpToAbout(event) {
    wx.navigateTo({
      url: '/pages/about/about',
    })
  },

  onStudy(event) {
    wx.navigateTo({
      url: '/pages/course/course',
    })
  }
 
})